//
//  SignInputViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/9.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "SignInputViewController.h"
#import "SignViewController.h"
#import "BaseNavigationController.h"
#import "VolunteerInputViewController.h"
@interface SignInputViewController ()

@end

@implementation SignInputViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view from its nib.
    [self setupNavigation];
    self.groupField.layer.borderWidth = 0.3;
    self.groupField.layer.cornerRadius = 5;
    self.groupField.layer.borderColor = [UIColor lightGrayColor].CGColor;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SVProgressHUDDidTouchDownInsideNotification
                                               object:nil];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:SVProgressHUDDidTouchDownInsideNotification object:nil];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
#pragma mark 点击取消等待界面
- (void)handleNotification:(NSNotification *)notif
{
    [SVProgressHUD dismiss];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.operationQueue cancelAllOperations];
}
-(void)setupNavigation
{
    UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];;
    [nextBtn setTitle:@"下一步" forState:0];
    [nextBtn setTitleColor:[UIColor whiteColor] forState:0];
    [nextBtn addTarget:self action:@selector(nextAction:) forControlEvents:64];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:nextBtn];
    
    if (self.pushStyle ==ActivityStyle) {
        self.title = @"输入活动代码";
        self.groupField.placeholder = @"请输入活动代码";
    }else
    {
        self.title = @"输入机构代码";
        
    }
    
    
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 25)];
    [backBtn setTitle:@"返回" forState:0];
    [backBtn setTitleColor:[UIColor whiteColor] forState:0];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:64];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    
}
-(void)backAction:(UIButton *)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void)nextAction:(UIButton *)sender
{
    
    [self.groupField resignFirstResponder];
    
    if (![self.groupField.text isEqualToString:@""]) {
        [SVProgressHUD showWithStatus:@"加载中.." maskType:SVProgressHUDMaskTypeBlack];
        
        if (self.pushStyle ==ActivityStyle)
        {
            
            [self getActivityInfo];
        }else
        {
            [self getGroupInfo];
        }
    }
    else
    {
        [Utils showAlert:@"所填内容不能为空"];
    }
    
        
//        NSDictionary *parameters = @{@"token":@"sdfsadfsadfasdf",@"module":@"VoluteerGroup",@"method":@"loadVoluteerGroupBycode",@"requestId":[NSNull new],@"requestParam":@[self.groupField.text]};
//        
//        [Utils requestDic:parameters andRusult:^(id result) {
//            if ([result isKindOfClass:[NSDictionary class]]) {
//                
//                if(![result[@"responseData"] isKindOfClass:[NSNull class]])
//                {
//                    [SVProgressHUD dismiss];
//                    NSDictionary *responseData = result[@"responseData"];
//                    if (responseData&&responseData.allKeys.count>0) {
//                    NSString *groupName = responseData[@"groupName"];
//                NSString *groupId = responseData[@"groupId"];
//                        switch (self.pushStyle) {
//                            case SignZbarStyle:
//                                [self pushSignViewController:groupName andGroupId:groupId];
//                                break;
//                            case MyZbarStyle:
//                            {
//                                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
//                                [SVProgressHUD showSuccessWithStatus:groupName maskType:SVProgressHUDMaskTypeBlack];
//                            }
//                                
//                                break;
//                            case ActivityStyle:
//                                [self pushInputViewController:groupName andGroupId:groupId];
//                                break;
//                                case ActivityGroupStyle:
//                                [self popInputViewController:groupName andGroupId:groupId];
//                                
//                        }
//                        //                        [self pushSignViewController:groupName andGroupId:groupId];
//                    }
//                }else
//                {
//                    [SVProgressHUD showErrorWithStatus:@"没有该机构" maskType:SVProgressHUDMaskTypeBlack];
//                }
//                
//                
//            }
//            
//        } andError:^(id result) {
//            [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
//            
//            
//        }];
//    }else
//    {
//
//    }
//    }else
//    {
//        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
//    }
    
    
}
#pragma mark 获取活动信息
-(void)getActivityInfo
{
    [[BuaatVolunteerServerSDK sharedInstance]requestQrcodeActivityInfoWithActivityCode:self.groupField.text success:^(NSHTTPURLResponse *response, id responseObject) {
        
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            id result = responseObject[@"result"];
            if ([result isKindOfClass:[NSDictionary class]]) {
//
                [self pushWithActivity:result];
//
//                
//                
            }else
            {
                
                [SVProgressHUD showErrorWithStatus:@"没有该活动信息" maskType:SVProgressHUDMaskTypeBlack];
            }
        }else
        {
            [SVProgressHUD showErrorWithStatus:@"没有该活动信息" maskType:SVProgressHUDMaskTypeBlack];
        }
        //        [self pushInput];
        
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
    }];
}


#pragma mark 获取机构信息
-(void)getGroupInfo
{
    [[BuaatVolunteerServerSDK sharedInstance]requestQrcodeGroupInfoWithGroupCode:self.groupField.text success:^(NSHTTPURLResponse *response, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            [SVProgressHUD dismiss];
            id result = responseObject[@"result"];
            if ([result isKindOfClass:[NSDictionary class]]) {
                NSString *groupCode = result[@"groupCode"];
                NSString *groupName = result[@"groupName"];
                
                switch (self.pushStyle) {
                    case SignZbarStyle:
                        [self pushSignViewController:groupName andGroupCode:groupCode];
                        break;
                    case MyZbarStyle:
                    {
                        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                        [SVProgressHUD showSuccessWithStatus:groupName maskType:SVProgressHUDMaskTypeBlack];
                    }
                        
                        break;
                    case ActivityStyle:
                        break;
                    case ActivityGroupStyle:
                        [self popInputViewController:groupName andGroupCode:groupCode];
                        
                }
            }else
            {
                [SVProgressHUD showErrorWithStatus:@"没有该机构" maskType:SVProgressHUDMaskTypeBlack];
            }
        }else
        {
            [SVProgressHUD showErrorWithStatus:@"没有该机构" maskType:SVProgressHUDMaskTypeBlack];
        }
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误" maskType:SVProgressHUDMaskTypeBlack];
        
    }];
}

#pragma mark 返回到补登界面
-(void)popInputViewController:(NSString *)groupName andGroupCode:(NSString *)groupCode
{
    [[NSNotificationCenter defaultCenter]postNotificationName:NotificationPopGroupInfo object:self userInfo:@{@"groupName":groupName,@"groupCode":groupCode}];
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark push到补登界面
-(void)pushWithActivity:(NSDictionary *)result
{
    VolunteerInputViewController *vInputVC = [[VolunteerInputViewController alloc]initWithNibName:@"VolunteerInputViewController" bundle:nil];
    [vInputVC setActivityDic:result];
    vInputVC.isAlreadyInputActiveCode = YES;
    [vInputVC setIsDismissVC:YES];
    [self.navigationController pushViewController:vInputVC animated:YES];
}
#pragma mark 推到注册界面
-(void)pushSignViewController:(NSString *)groupName andGroupCode:(NSString *)groupCode
{
    SignViewController *signVC = [[SignViewController alloc]initWithNibName:@"SignViewController" bundle:nil];
    [signVC setGroupCode:groupCode];
    [signVC setGroupName:groupName];
    [self.navigationController pushViewController:signVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
